def max_profit(prices):
    holding, cooling, just_sold = -prices[0], 0, 0
    for price in prices[1:]:
        holding, cooling, just_sold = max(holding, cooling - price), max(cooling, just_sold), holding + price
    return max(cooling, just_sold)